import {defaultLoginApi,getCurrentUserApi,updateLoginApi} from "@/api/user"
import {resetRouter} from "@/router";

const state = {
    name: '',
    role: ''
}

const mutations = {
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_ROLE: (state, role) => {
        state.role = role
    }
}

const actions = {
    // user login
    defaultLogin({ commit }, data) {
        console.log('user/defaultLogin', data)
        return new Promise((resolve, reject) => {
            defaultLoginApi(data).then(res => {
            //   console.log('defaultLogin', res);
              if (res.status === 10000) {
                resolve({
                    type: "success",
                    message: res.msg
                })
            } else {
                reject({
                    type: "error",
                    message: res.msg
                })
            }
        }).catch(error => {
            reject({
                type: "error",
                message: error.msg
            })
        })
    },  
        )
},
updateLogin({ commit }, data) {
    console.log('user/defaultLogin', data)
    return new Promise((resolve, reject) => {
        updateLoginApi(data).then(res => {
        //   console.log('defaultLogin', res);
          if (res.status === 10000) {
            resolve({
                type: "success",
                message: res.msg
            })
        } else {
            reject({
                type: "error",
                message: res.msg
            })
        }
    }).catch(error => {
        reject({
            type: "error",
            message: error.msg
        })
    })
},  
    )
},
getCurrentUser({ commit }, data) {
    //console.log('user/defaultLogin', data)
    return new Promise((resolve, reject) => {
        getCurrentUserApi(data).then(res => {
        //   console.log('defaultLogin', res);
          if (res.status === 10000) {
            resolve({
                type: "success",
                message: res.msg,
                data:res.data
            })
        } else {
            reject({
                type: "error",
                message: res.msg
            })
        }
    }).catch(error => {
        reject({
            type: "error",
            message: error.msg
        })
    })
},  
    )
}
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
